﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;

using HostsManager.Forms;

namespace HostsManager
{
    static class Program
    {
        static Form1 form = null;

        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            NotifyIcon trayIcon = new NotifyIcon();
            trayIcon.Text = global::HostsManager.strings.ApplicationName;
            trayIcon.Icon = strings.AppIcon;
            trayIcon.Visible = true;

            form = new Form1();
            form.ExitApplication += new Form1.ExitApplicationEventHandler(OnApplicationExit);

            TrayIconContextMenu contextMenu = new TrayIconContextMenu();
            contextMenu.ExitApplication += new TrayIconContextMenu.ExitApplicationEventHandler(OnApplicationExit);
            contextMenu.ShowForm += new TrayIconContextMenu.ShowFormEventHandler(OnShowForm);

            trayIcon.ContextMenu = contextMenu;
            trayIcon.Visible = true;

            trayIcon.DoubleClick += new EventHandler(OnToggleFormVisibility);

            Application.Run();

            trayIcon.Visible = false;
        }

        static void OnShowForm(object sender)
        {
            form.Show();
        }

        static void OnApplicationExit(object sender)
        {
            form.Close();
            form.Dispose();

            Application.Exit();
        }

        static void OnToggleFormVisibility(object sender, EventArgs e)
        {
            if (form != null)
            {
                form.Visible = !form.Visible;
            }
        }
    }
}
